package codetop.greedy;

import java.util.Arrays;

public class Code1658 {
    public int minOperations(int[] nums, int x) {
        int right=0;
        int rSum= Arrays.stream(nums).sum();
        int lSum=0;
        int ans=Integer.MAX_VALUE;
        for (int left=-1; left <nums.length ; left++) {
            if (left!=-1)lSum+=nums[left];

            while (right<nums.length&&lSum+rSum>x){
                rSum-=nums[right];
                right++;
            }
            if (lSum+rSum==x){
                ans=Math.min(nums.length-right+left+1,ans);
            }
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }
}
